// pages/business/mykucun/mykucun.js
var app = getApp();
var $ = app.globalData;
Page({
	data: {
		page: 1,
		goodslist:[],
	},
	onLoad: function(options) {
		var that = this
		that.getmykucun()
	},
	// 加载更多
	loadgengduo:function(){
		var that = this
		var page = that.data.page+1
		that.setData({
			page:page
		})
		that.getmykucun()
	},
	// 删除库存
	shanchu:function(e){
		var that = this
		var thatid = e.currentTarget.dataset.id
		var thatindex = e.currentTarget.dataset.index
		wx.request({
			url:$.AdminUrl+"/YApi/delShopGoods",
			data:{
				shopid:$.myshopid,
				goodsid:thatid
			},
			success:function(res){
				console.log(res)
				
				
				if(res.data.code){
					wx.showToast({
						icon:"none",
					    title: "删除成功",
					    duration: 3e3
					})
				}else{
					wx.showToast({
						icon:"none",
					    title: res.data.msg,
					    duration: 3e3
					})
				}
				setTimeout(function(){
					var goodslist = []
					that.setData({
						goodslist:goodslist
					})
					that.onLoad()
				},3000)
			}
		})
	},
	// 获取我的库存
	getmykucun:function(){
		var that = this
		wx.request({
			url:$.AdminUrl+"/YApi/myGoodslib",
			data:{
				shopid:$.myshopid,
				name:'',
				page:that.data.page
			},
			success:function(data){
				var goodslist = that.data.goodslist
				var newlist = data.data.obj
				goodslist = goodslist.concat(newlist)
				var loadmore = newlist.length >= 10?true:false
				that.setData({
					goodslist:goodslist,
					loadmore:loadmore
				})
			}
		})
	},
	// 上架
	upjia:function(e){
		var that = this
		var thatid = e.currentTarget.dataset.id
		var thatindex = e.currentTarget.dataset.index
		var goodslist = that.data.goodslist
		wx.request({
			url:$.AdminUrl+"/YApi/changeShopGoodsState",
			data:{
				shopid:$.myshopid,
				goodsid:thatid,
				state:0,
				stock:'',
				sortnum:'',
				fareprice:''
			},
			success:function (res) {
				console.log(res)
				if(res.data.code){
					wx.showToast({
						icon:"none",
					    title: "上架成功",
					    duration: 3e3
					})
					goodslist[thatindex].state = 0
					that.setData({
						goodslist:goodslist
					})
				}else{
					wx.showToast({
						icon:"none",
					    title: res.data.msg,
					    duration: 3e3
					})
				}
				
			}
		})
	},
	downjia:function(e){
		var that = this
		var thatid = e.currentTarget.dataset.id
		var thatindex = e.currentTarget.dataset.index
		var goodslist = that.data.goodslist
		wx.request({
			url:$.AdminUrl+"/YApi/changeShopGoodsState",
			data:{
				shopid:$.myshopid,
				goodsid:thatid,
				state:1,
				stock:'',
				sortnum:'',
				fareprice:''
			},
			success:function (res) {
				console.log(res)
				if(res.data.code){
					wx.showToast({
						icon:"none",
					    title:  "下架成功",
					    duration: 3e3
					})
					goodslist[thatindex].state = 1
					that.setData({
						goodslist:goodslist
					})
				}else{
					wx.showToast({
						icon:"none",
					    title: res.data.msg,
					    duration: 3e3
					})
				}
				
			}
		})
	}
})





// stock
// sortnum
// fareprice
